package mysql

import (
	"com-man-sys1/goodsrpc/goodsrpc"
	"gorm.io/gorm"
)

type Goods struct {
	gorm.Model
	GoodsName  string  `form:"type:varchar(30);not null;comment:'商品名称'"`
	GoodsPrice float64 `form:"type:decimal(10,2);not null;comment:'商品价格'"`
	GoodsCode  string  `form:"type:varchar(100);not null;comment:'货号'"`
	FromPeople string  `form:"type:varchar(100);not null;comment:'供应商'"`
	UserId     int     `form:"type:int(11);not null;comment:'用户ID'"`
}

func (g *Goods) FirstGoodsByName(db *gorm.DB, name string) error {
	return db.Debug().Where("goods_name=?", name).First(g).Error
}

func (g *Goods) GoodsAdd(db *gorm.DB) error {
	return db.Debug().Create(g).Error
}

func GoodsList(db *gorm.DB, id int64) ([]*goodsrpc.GoodsList, error) {
	var goods []*goodsrpc.GoodsList

	err2 := db.Debug().Where("goods.user_id=?", id).Model(&Goods{}).Select(
		"*",
	).Joins("LEFT JOIN users ON goods.user_id=users.id").Find(&goods).Error

	return goods, err2
}
